// pages/discovery/discovery.js
var app = getApp();
Page({
  data: {
    array: [],
    hidden: "hidden",
    next: '',
    list: [
      { id: 0, 'type': 'all', name: '校园要闻', storage: [], url: 'xwzx/xxyw.htm', enabeld: { guest: true, student: true, teacher: true } },
      { id: 1, 'type': 'jw', name: '校园快讯', storage: [], url: 'xwzx/xykx.htm', enabled: { guest: true, student: true, teacher: true } },
      { id: 2, 'type': 'jz', name: '媒体关注', storage: [], url: 'xwzx/mtgz.htm', enabled: { guest: true, student: true, teacher: true } },
      { id: 3, 'type': 'new', name: '通知公告', storage: [], url: 'xwzx/tzgg.htm', enabled: { guest: true, student: true, teacher: true } },
      { id: 4, 'type': 'oa', name: '科研动态', stroage: [], url: 'xwzx/kydt.htm', enabled: { guest: true, student: true, teacher: true } },
      { id: 5, 'type': 'hy', name: '高职关注', storage: [], url: 'xwzx/gzzx.htm', enabled: { guest: true, student: true, teacher: true } }
    ],
    loading: false,
    uesr_type: 'guest',  // 默认为访客
    disabledRemind: false
  },
  onShareAppMessage: function () {

  },
  get_news_list: function (hrefs, href) {
    var that = this;
      wx.showLoading({
        title: '获取新闻列表中',
      });
      wx.cloud.callFunction({
        name: "school_news",
        data: {
          type: 'get_news_title',
          href: href,
          hrefs: hrefs
        },
        success(res) {
          wx.hideLoading();
          if (res.result.error == "0") {
            var data = res.result.data;
            console.log(data);
            that.setData({
              array: that.data.array.concat(data),
              hidden: '',
              next: res.result.next
            })
            app.globalData.news_json = that.data.array;
          }
          else {
            wx.showModal({
              content: '获取新闻失败',
              showCancel: false
            });
          }
        },
        fail(res) {
          wx.hideLoading();
          wx.showModal({
            content: '获取新闻超时',
            showCancel: false
          });
        }
      })
  },
  onLoad: function () {
    console.log("test");
    this.get_news_list('xwzx/xxyw.htm', "");
    this.setData({
      user_type: 'guest',
      'active.id': 5,
      'activetype': 'new'
    });
    this.setData({
      'active': {
        'id': 0,
        'type': 'all',
        data: [],
        showMore: true,
        remind: '上滑加载更多'
      },
      'page': 0
    });
  },
  getnext: function () {
    this.setData({
      hidden: 'hidden'
    })
    if (this.data.next == "" || this.data.next == undefined) {
      wx.showModal({
        content: '新闻已加载完成',
        showCancel: false
      });
    }
    else {
      this.get_news_list("", this.data.next);
    }
  },
  onShow: function () {

  },
  changeFilter: function (e) {
    console.log(e);
    var url = e.target.dataset.url;
    this.setData({
      array: [],
      next: '',
      hidden: "hidden"
    })
    this.setData({
      'active': {
        'id': e.target.dataset.id,
        'type': e.target.id,
        data: [],
        showMore: true,
        remind: '上滑加载更多'
      },
      'page': 0
    });
    this.get_news_list(url, "");
  },
  onPullDownRefresh: function () {
    var that = this;
    this.setData({
      array: [],
      next: '',
      hidden: "hidden"
    })
    this.get_news_list('xwzx/xxyw.htm', "");
    this.setData({
      user_type: 'guest',
      'active.id': 5,
      'activetype': 'new'
    });
    this.setData({
      'active': {
        'id': 0,
        'type': 'all',
        data: [],
        showMore: true,
        remind: '上滑加载更多'
      },
      'page': 0
    });
    wx.stopPullDownRefresh();
  },
})